package Service;

import Entity.Customer;
import Mapper.CustomerMapper;
import Service.CustomerService;
import java.time.LocalDateTime;
import java.util.List;

public class CustomerServiceImpl implements CustomerService {

    private CustomerMapper customerMapper;

    public void setCustomerMapper(CustomerMapper customerMapper) {
        this.customerMapper = customerMapper;
    }

    @Override
    public boolean save(Customer customer) {
        // 基本校验
        if (customer.getName() == null || customer.getName().trim().isEmpty()) {
            throw new IllegalArgumentException("客户姓名不能为空");
        }
        if (customer.getPhone() == null || customer.getPhone().trim().isEmpty()) {
            throw new IllegalArgumentException("客户电话不能为空");
        }

        // 设置创建时间
        if (customer.getCreate_time() == null) {
            customer.setCreate_time(LocalDateTime.now());
        }

        return customerMapper.insert(customer) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return customerMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(Customer customer) {
        if (customer.getId() == null) {
            throw new IllegalArgumentException("更新客户必须指定ID");
        }
        return customerMapper.update(customer) > 0;
    }

    @Override
    public Customer getById(Integer id) {
        return customerMapper.selectById(id);
    }

    @Override
    public List<Customer> getAll() {
        return customerMapper.selectAll();
    }

    @Override
    public List<Customer> getByName(String name) {
        return customerMapper.selectByName(name);
    }

    @Override
    public List<Customer> getByPhone(String phone) {
        return customerMapper.selectByPhone(phone);
    }

    @Override
    public List<Customer> getByEmail(String email) {
        return customerMapper.selectByEmail(email);
    }
}